<template>
  <div class="subject">
    <!-- 搜索栏表单 -->
    <el-card>
      <el-form :model="form" ref="form" inline>
        <el-form-item prop="rid" label="学科编号">
          <el-input v-model="form.rid" class="w100"></el-input>
        </el-form-item>
        <el-form-item prop="name" label="学科名称">
          <el-input v-model="form.name" class="w100"></el-input>
        </el-form-item>
        <el-form-item prop="username" label="创建则">
          <el-input v-model="form.username" class="w100"></el-input>
        </el-form-item>
        <el-form-item prop="status" label="状态">
          <el-select v-model="form.status" class="w100">
            <el-option label="禁用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="clear">清除</el-button>
          <el-button type="primary" @click="addSubject">+新增学科</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <!-- 主体表格 -->
      <el-table :data="tableData">
        <el-table-column label="序号" width="60px">
          <template v-slot="scope">{{scope.$index +1}}</template>
        </el-table-column>
        <el-table-column label="学科编号" prop="rid"></el-table-column>
        <el-table-column label="学科名称" prop="name"></el-table-column>
        <el-table-column label="简称" prop="short_name"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期" prop="create_time"></el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">{{scope.row.status == 0?'禁用':'启用'}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button
              type="text"
              @click="disable(scope.row.id)"
            >{{scope.row.status == 1?'禁用':'启用'}}</el-button>
            <el-button type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="[1, 2, 3, 4]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.totalItem"
          background
        ></el-pagination>
      </div>
    </el-card>
    <subjectAdd ref="subjectAdd" :mode="modes" :type="types" @sonEdit="getData"></subjectAdd>
  </div>
</template>
<script>
import {
  getSubjectList,
  setSubjectStatus,
  removeSubject
} from "@/api/subject.js";
export default {
  components: {
    subjectAdd: () => import("./subjectAdd")
  },
  data() {
    return {
      modes: null,
      types: null,
      form: {
        rid: "", // 	否	string	学科编号
        name: "", // 	否	string	学科名称
        username: "", // 	否	string	创建者用户名
        status: "", // 	否	string	状态 0(禁用) 1(启用)
        page: "", // 	否	string	页码 默认为1
        limit: "" // 	否	string	页尺寸 默认为10
      },
      tableData: [],
      pagination: {
        currentPage: 1,
        pageSize: 1,
        totalItem: 10
      }
    };
  },
  methods: {
    edit(row) {
      this.types = "编辑学科";
      this.modes = "edit";
      this.$refs.subjectAdd.isShow = true;
      this.$refs.subjectAdd.form = JSON.parse(JSON.stringify(row));
    },
    del(id) {
      removeSubject({ id }).then(() => {
        this.$message.success("删除成功!");
        this.search();
      });
    },
    disable(id) {
      setSubjectStatus({ id }).then(() => {
        this.$message.success("设置成功!");
        this.getData();
      });
    },
    // 搜索栏清除按钮事件
    clear() {
      this.$refs.form.resetFields();
      this.getData();
    },
    // 页容量选项改变触发事件
    handleSizeChange(size) {
      this.pagination.pageSize = size;
      this.pagination.currentPage = 1;
      this.getData();
    },
    // 页码改变触发事件
    handleCurrentChange(page) {
      this.pagination.currentPage = page;
      this.getData();
    },
    // 新增学科面板
    addSubject() {
      this.types = "新增学科";
      this.modes = "add";
      this.$refs.subjectAdd.isShow = true;
    },
    // 封装： 获取学科列表数据
    getData() {
      let _query = {
        ...this.form,
        page: this.pagination.currentPage,
        limit: this.pagination.pageSize
      };
      getSubjectList(_query).then(res => {
        this.tableData = res.data.items;
        this.pagination.totalItem = res.data.pagination.total;
        this.tableData.forEach(item => {
          item.create_time = item.create_time.split(" ")[0];
        });
      });
    },
    search() {
      this.pagination.currentPage = 1;
      this.getData();
    }
  },
  created() {
    this.getData();
  }
};
</script>
<style lang="less">
.subject {
  .w100 {
    width: 160px;
  }
  .pagination {
    text-align: center;
    padding-top: 30px;
  }
}
</style>